//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Data;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Mail;
using DotNetNuke.Services.Vendors;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.Modules.Admin.Vendors
{
	partial class EditBanner : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		private int VendorId = Null.NullInteger;
		protected System.Web.UI.WebControls.Label lblBannerGroup;
		private int BannerId = Null.NullInteger;
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdCopy.Click += new EventHandler(cmdCopy_Click);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			cmdEmail.Click += new EventHandler(cmdEmail_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);
			DNNTxtBannerGroup.PopulateOnDemand += new DotNetNuke.UI.WebControls.DNNTextSuggest.DNNTextSuggestEventHandler(DNNTxtBannerGroup_PopulateOnDemand);

			try {
				if ((Request.QueryString["VendorId"] != null)) {
					VendorId = Int32.Parse(Request.QueryString["VendorId"]);
				}
				if ((Request.QueryString["BannerId"] != null)) {
					BannerId = Int32.Parse(Request.QueryString["BannerId"]);
				}
				cmdStartCalendar.NavigateUrl = Common.Utilities.Calendar.InvokePopupCal(txtStartDate);
				cmdEndCalendar.NavigateUrl = Common.Utilities.Calendar.InvokePopupCal(txtEndDate);
				if (Page.IsPostBack == false) {
					ctlImage.FileFilter = Common.Globals.glbImageFileTypes;
					ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteItem"));
					Services.Vendors.BannerTypeController objBannerTypes = new Services.Vendors.BannerTypeController();
					cboBannerType.DataSource = objBannerTypes.GetBannerTypes();
					cboBannerType.DataBind();
					BannerController objBanners = new BannerController();
					if (BannerId != Null.NullInteger) {
						BannerInfo objBanner = objBanners.GetBanner(BannerId);
						if (objBanner != null) {
							txtBannerName.Text = objBanner.BannerName;
							cboBannerType.Items.FindByValue(objBanner.BannerTypeId.ToString()).Selected = true;
							DNNTxtBannerGroup.Text = objBanner.GroupName;
							ctlImage.Url = objBanner.ImageFile;
							if (objBanner.Width != 0) {
								txtWidth.Text = objBanner.Width.ToString();
							}
							if (objBanner.Height != 0) {
								txtHeight.Text = objBanner.Height.ToString();
							}
							txtDescription.Text = objBanner.Description;
							if (!String.IsNullOrEmpty(objBanner.URL)) {
								ctlURL.Url = objBanner.URL;
							}
							txtImpressions.Text = objBanner.Impressions.ToString();
							txtCPM.Text = objBanner.CPM.ToString();
							if (!Null.IsNull(objBanner.StartDate)) {
								txtStartDate.Text = objBanner.StartDate.ToShortDateString();
							}
							if (!Null.IsNull(objBanner.EndDate)) {
								txtEndDate.Text = objBanner.EndDate.ToShortDateString();
							}
							optCriteria.Items.FindByValue(objBanner.Criteria.ToString()).Selected = true;
							ctlAudit.CreatedByUser = objBanner.CreatedByUser;
							ctlAudit.CreatedDate = objBanner.CreatedDate.ToString();
							ArrayList arrBanners = new ArrayList();
							arrBanners.Add(objBanner);
							lstBanners.DataSource = arrBanners;
							lstBanners.DataBind();
						} else {
							Response.Redirect(EditUrl("VendorId", VendorId.ToString()), true);
						}
					} else {
						txtImpressions.Text = "0";
						txtCPM.Text = "0";
						optCriteria.Items.FindByValue("1").Selected = true;
						cmdDelete.Visible = false;
						cmdCopy.Visible = false;
						cmdEmail.Visible = false;
						ctlAudit.Visible = false;
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdCancel_Click(object sender, EventArgs e)
		{
			try {
				Response.Redirect(EditUrl("VendorId", VendorId.ToString()), true);
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdDelete_Click(object sender, EventArgs e)
		{
			try {
				if (BannerId != -1) {
					Services.Vendors.BannerController objBanner = new Services.Vendors.BannerController();
					objBanner.DeleteBanner(BannerId);
					Response.Redirect(EditUrl("VendorId", VendorId.ToString()), true);
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try {
				if (Page.IsValid == true) {
					if (!cmdCopy.Visible) {
						BannerId = -1;
					}
					System.DateTime StartDate = Null.NullDate;
					if (!String.IsNullOrEmpty(txtStartDate.Text)) {
						StartDate = Convert.ToDateTime(txtStartDate.Text);
					}
					System.DateTime EndDate = Null.NullDate;
					if (!String.IsNullOrEmpty(txtEndDate.Text)) {
						EndDate = Convert.ToDateTime(txtEndDate.Text);
					}
					BannerInfo objBanner = new BannerInfo();
					objBanner.BannerId = BannerId;
					objBanner.VendorId = VendorId;
					objBanner.BannerName = txtBannerName.Text;
					objBanner.BannerTypeId = Convert.ToInt32(cboBannerType.SelectedItem.Value);
					objBanner.GroupName = DNNTxtBannerGroup.Text;
					objBanner.ImageFile = ctlImage.Url;
					if (!String.IsNullOrEmpty(txtWidth.Text)) {
						objBanner.Width = int.Parse(txtWidth.Text);
					} else {
						objBanner.Width = 0;
					}
					if (!String.IsNullOrEmpty(txtHeight.Text)) {
						objBanner.Height = int.Parse(txtHeight.Text);
					} else {
						objBanner.Height = 0;
					}
					objBanner.Description = txtDescription.Text;
					objBanner.URL = ctlURL.Url;
					objBanner.Impressions = int.Parse(txtImpressions.Text);
					objBanner.CPM = double.Parse(txtCPM.Text);
					objBanner.StartDate = StartDate;
					objBanner.EndDate = EndDate;
					objBanner.Criteria = int.Parse(optCriteria.SelectedItem.Value);
					objBanner.CreatedByUser = UserInfo.UserID.ToString();
					Services.Vendors.BannerController objBanners = new Services.Vendors.BannerController();
					if (BannerId == Null.NullInteger) {
						objBanners.AddBanner(objBanner);
					} else {
						objBanners.UpdateBanner(objBanner);
					}
					Response.Redirect(EditUrl("VendorId", VendorId.ToString()), true);
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdCopy_Click(object sender, System.EventArgs e)
		{
			try {
				txtStartDate.Text = "";
				txtEndDate.Text = "";
				cmdDelete.Visible = false;
				cmdCopy.Visible = false;
				cmdEmail.Visible = false;
				ctlAudit.Visible = false;
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdEmail_Click(object sender, System.EventArgs e)
		{
			BannerController objBanners = new BannerController();
			BannerInfo objBanner = objBanners.GetBanner(BannerId);
			if (objBanner != null) {
				VendorController objVendors = new VendorController();
				VendorInfo objVendor = objVendors.GetVendor(objBanner.VendorId, PortalId);
				if (objVendor != null) {
					if (!Null.IsNull(objVendor.Email)) {
						ArrayList Custom = new ArrayList();
						Custom.Add(objBanner.BannerName);
						Custom.Add(objBanner.Description);
						Custom.Add(objBanner.ImageFile);
						Custom.Add(objBanner.CPM.ToString("#0.#####"));
						Custom.Add(objBanner.Impressions.ToString());
						Custom.Add(objBanner.StartDate.ToShortDateString());
						Custom.Add(objBanner.EndDate.ToShortDateString());
						Custom.Add(objBanner.Views.ToString());
						Custom.Add(objBanner.ClickThroughs.ToString());
						string errorMsg = Mail.SendMail(PortalSettings.Email, objVendor.Email, "", Services.Localization.Localization.GetSystemMessage(PortalSettings, "EMAIL_BANNER_NOTIFICATION_SUBJECT", Services.Localization.Localization.GlobalResourceFile, Custom), Services.Localization.Localization.GetSystemMessage(PortalSettings, "EMAIL_BANNER_NOTIFICATION_BODY", Services.Localization.Localization.GlobalResourceFile, Custom), "", "", "", "", "",
						"");
						string strMessage;
						if (String.IsNullOrEmpty(errorMsg)) {
							strMessage = Services.Localization.Localization.GetString("EmailSuccess", this.LocalResourceFile);
							UI.Skins.Skin.AddModuleMessage(this, strMessage, UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
						} else {
							strMessage = Services.Localization.Localization.GetString("EmailFailure", this.LocalResourceFile);
							strMessage = string.Format(strMessage, errorMsg);
							UI.Skins.Skin.AddModuleMessage(this, strMessage, UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
						}
					}
				}
			}
		}
		protected void DNNTxtBannerGroup_PopulateOnDemand(object source, UI.WebControls.DNNTextSuggestEventArgs e)
		{
			DataTable dt;
			DotNetNuke.UI.WebControls.DNNNode objNode;
			BannerController objBanners = new BannerController();
			dt = objBanners.GetBannerGroups(PortalId);
			DataRow[] dr;
			dt.CaseSensitive = false;
			dr = dt.Select("GroupName like '" + e.Text + "%'");
			foreach (DataRow d in dr) {
				objNode = new DotNetNuke.UI.WebControls.DNNNode(d["GroupName"].ToString());
				objNode.ID = e.Nodes.Count.ToString();
				e.Nodes.Add(objNode);
			}
		}
		public string FormatItem(int VendorId, int BannerId, int BannerTypeId, string BannerName, string ImageFile, string Description, string URL, int Width, int Height)
		{
			BannerController objBanners = new BannerController();
			return objBanners.FormatBanner(VendorId, BannerId, BannerTypeId, BannerName, ImageFile, Description, URL, Width, Height, PortalId == -1 ? "G" : "L",
			PortalSettings.HomeDirectory);
		}
		public new int PortalId {
			get {
				if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId) {
					return -1;
				} else {
					return PortalSettings.PortalId;
				}
			}
		}
	}
}
